package com.example.urlapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class registerActivity2 extends AppCompatActivity {
    private String registerUrl = "https://www.wanandroid.com/user/register";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register2);
    }

    public void click1(View view) {
        EditText user=findViewById(R.id.suser);
        String u = user.getText().toString();
        EditText password=findViewById(R.id.spassword);
        String p = password.getText().toString();
        EditText repassword=findViewById(R.id.srepassword);
        String re = repassword.getText().toString();
        new Thread(){
            @Override
            public void run(){
                try {
                    String ticket=register(u,p,re);
                    JSONObject js=new JSONObject(ticket);
                    int errorCode=js.optInt("errorCode");
                    if(errorCode==0){
                        System.out.println("注册成功");
                    }
                    else {
                        System.out.println("注册失败");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        .start();
    }
    private String register(String username,String pwd,String repwd){
        try {
            URL url = new URL(registerUrl);
            HttpURLConnection cn = (HttpURLConnection) url.openConnection();
            cn.setRequestMethod("POST");
            cn.setDoOutput(true);
            cn.setUseCaches(false);
            String str = "username=" + URLEncoder.encode(username, "utf-8")
                    + "&password=" + URLEncoder.encode(pwd, "utf-8")
                    + "&repassword=" + URLEncoder.encode(repwd, "utf-8");
            OutputStream outputStream = cn.getOutputStream();
            outputStream.write(str.getBytes());
            outputStream.flush();
            if (cn.getResponseCode()==200){
                InputStream inputStream = cn.getInputStream();
                String s = Inputstreamer(inputStream);
                return s;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    private String Inputstreamer(InputStream inputStream){
        BufferedReader bf=new BufferedReader(new InputStreamReader(inputStream));
        StringBuffer sb=new StringBuffer();
        String Line;
        try {
            while ((Line=bf.readLine())!=null){
                sb.append(Line);
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return sb.toString();
    }
}